{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from QKDNetwork import QKDNetwork\n",
    "from compare import Compare\n",
    "import concurrent.futures"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 节点个数： 20-210          默认：60      迭代：20 + cnt * 10\n",
    "# 源目对数： 10-200          默认：50      迭代：10 + cnt * 10\n",
    "# 阿尔法值： 0.35-1.3        默认：0.35     迭代：0.2 + cnt * 0.05\n",
    "\n",
    "def experiment(num_node, sd_num, alpha):\n",
    "    try:\n",
    "        q = QKDNetwork(showTopology=False, num_node=num_node, sd_num=sd_num, alpha=alpha)\n",
    "        c = Compare(q)\n",
    "        return c.getData()\n",
    "    except Exception as e:\n",
    "        print(\"No Feasible, Pass\")\n",
    "        return None\n",
    "\n",
    "def run_experiments_node_variation():\n",
    "    total_tmp_var_node_num = []\n",
    "    for round_idx in range(50):\n",
    "        round_tmp = []\n",
    "        num_node = 20 + round_idx * 10\n",
    "        sd_num = 50\n",
    "        alpha = 0.2\n",
    "        print(f\"num_node={num_node}, sd_num={sd_num}, alpha={alpha}\")\n",
    "        with concurrent.futures.ThreadPoolExecutor(max_workers=16) as executor:\n",
    "            future_list = [\n",
    "                executor.submit(experiment, num_node, sd_num, alpha) \n",
    "                for _ in range(20)\n",
    "            ]\n",
    "            for future in concurrent.futures.as_completed(future_list, timeout=600):\n",
    "                data = future.result()\n",
    "                if data is not None:\n",
    "                    round_tmp.append(data)\n",
    "                    print(data)\n",
    "                else:\n",
    "                    break\n",
    "        total_tmp_var_node_num.append(round_tmp)\n",
    "        if not round_tmp:  # If no results were obtained, stop further iterations.\n",
    "            break\n",
    "    print(total_tmp_var_node_num)\n",
    "\n",
    "def run_experiments_sd_variation():\n",
    "    total_tmp_var_sd_num = []\n",
    "    for round_idx in range(50):\n",
    "        round_tmp = []\n",
    "        num_node = 60\n",
    "        sd_num = 10 + round_idx * 10\n",
    "        alpha = 0.2\n",
    "        print(f\"num_node={num_node}, sd_num={sd_num}, alpha={alpha}\")\n",
    "        with concurrent.futures.ThreadPoolExecutor(max_workers=16) as executor:\n",
    "            future_list = [\n",
    "                executor.submit(experiment, num_node, sd_num, alpha) \n",
    "                for _ in range(20)\n",
    "            ]\n",
    "            for future in concurrent.futures.as_completed(future_list, timeout=600):\n",
    "                data = future.result()\n",
    "                if data is not None:\n",
    "                    round_tmp.append(data)\n",
    "                    print(data)\n",
    "                else:\n",
    "                    break\n",
    "        total_tmp_var_sd_num.append(round_tmp)\n",
    "        if not round_tmp:\n",
    "            break\n",
    "    print(total_tmp_var_sd_num)\n",
    "\n",
    "def run_experiments_alpha_variation():\n",
    "    total_tmp_var_alpha = []\n",
    "    for round_idx in range(50):\n",
    "        round_tmp = []\n",
    "        num_node = 60\n",
    "        sd_num = 50\n",
    "        alpha = 0.2 + round_idx * 0.05\n",
    "        print(f\"num_node={num_node}, sd_num={sd_num}, alpha={alpha}\")\n",
    "        with concurrent.futures.ThreadPoolExecutor(max_workers=16) as executor:\n",
    "            future_list = [\n",
    "                executor.submit(experiment, num_node, sd_num, alpha) \n",
    "                for _ in range(20)\n",
    "            ]\n",
    "            for future in concurrent.futures.as_completed(future_list, timeout=600):\n",
    "                data = future.result()\n",
    "                if data is not None:\n",
    "                    round_tmp.append(data)\n",
    "                    print(data)\n",
    "                else:\n",
    "                    break\n",
    "        total_tmp_var_alpha.append(round_tmp)\n",
    "        if not round_tmp:\n",
    "            break\n",
    "    print(total_tmp_var_alpha)\n",
    "\n",
    "# Start the experiments\n",
    "run_experiments_node_variation()\n",
    "run_experiments_sd_variation()\n",
    "run_experiments_alpha_variation()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "xs",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
